<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="newsList"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange">
      <el-table-column
        align="center"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="id"
        label="ID"
        width="200">
      </el-table-column>
      <el-table-column
        align="center"
        prop="title"
        label="标题">
      </el-table-column>
      <el-table-column
        align="center"
        prop="typename"
        label="分类"
        width="200"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="keyword"
        label="关键词"
        width="120">
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="batchDel()">批量删除</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>
    <el-pagination
      v-show="isShowPagination"
      background
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'news-list',
  data () {
    return {
      newsList: [],
      multipleSelection: [],
      total: 0,
      isShowPagination: false
    }
  },

  methods: {
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    getNewsList (pageNumber) {
      this.$http.get('/news/getNewsList', {
        params: {
          pageNumber: pageNumber
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          this.total = data.data.totalRow
          if (this.total > 0) {
            this.isShowPagination = true
          }
          this.newsList = data.data.list
        }
      })
    },
    handleCurrentChange (val) {
      this.getNewsList(val)
    },
    // 批量删除
    batchDel () {
      if (this.multipleSelection) {
        let ids = this.multipleSelection.map(item => item.id).join()
        if (ids.length > 0) {
          this.$http.get('/news/deleteNews', {
            params: {
              ids: ids
            }
          }).then(res => {
            let data = res.data
            let status = data.status
            if (status === 200) {
              this.getNewsList(1)
            }
          })
        }
      }
    },
    handleDelete (index, row) {
      this.$http.get('/news/deleteNews', {
        params: {
          ids: row.id
        }
      }).then(res => {
        let data = res.data
        let status = data.status
        if (status === 200) {
          this.getNewsList(1)
        }
      })
    },
    handleEdit (index, row) {
      this.$router.push({
        name: 'EditNews',
        params: {
          id: row.id,
          title: row.title,
          typename: row.typename,
          newsTypeId: row.news_type_id,
          sort: row.sort,
          keyword: row.keyword,
          imgurl: row.imgurl,
          content: row.content
        }
      })
    }
  },
  mounted () {
    this.getNewsList(1)
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.el-pagination{
  margin-top 50px
  text-align center
}
</style>
